package jp.com.nobnak.commentviewer.live;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.text.TextUtils;

public class PlayerStatusSaxHandler extends DefaultHandler {
	static public final String LOG_TAG = "PlayerStatusSAXHandler";
	static public final String DELIMITER = ".";
	
	private Stack<String> mName = new Stack<String>(); 
	private LiveStatus mResult;
	
	public ILiveStatus getResult() {
		return mResult;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		mResult.put(TextUtils.join(DELIMITER, mName), new String(ch, start, length));
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		mName.pop();
		super.endElement(uri, localName, qName);
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		mName.push(qName);
	}
	
	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		mResult = new LiveStatus();
	}

	@Override
	public void endDocument() throws SAXException {
		
		super.endDocument();
	}



	static private class LiveStatus implements ILiveStatus {
		static public final String CODE = "getplayerstatus.error.code";
		static public final String MS_ADDR = "getplayerstatus.ms.addr";
		static public final String MS_PORT = "getplayerstatus.ms.port";
		static public final String MS_THREAD = "getplayerstatus.ms.thread";


		private Map<String, String> mData = new HashMap<String, String>();
		
		public void put(String key, String value) {
			mData.put(key, value);
		}

		@Override
		public String getErrorCode() { return mData.get(CODE); }
		@Override
		public String getMessageServerAddress() { return mData.get(MS_ADDR); }
		@Override
		public String getMessageServerPort() { return mData.get(MS_PORT); }
		@Override
		public String getMessageServerThread() { return mData.get(MS_THREAD); }
	}
}
